package com.sgb.kjwl.view.ui.adapter.transport;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.CargoPublishEntity;
import com.sgb.kjwl.model.entity.transport.SingleItemConstant;
import com.sgb.kjwl.utils.DefaultUtil;

import java.util.List;

/**
 * @author: 杨畔
 * @date: 2021/7/15 15:20
 * @Description: 竞价货源联系人适配器
 */
public class BiddingCargoPublishContactAdapter extends BaseMultiItemQuickAdapter<CargoPublishEntity.ContactInfoEntity, BaseViewHolder> {
    //焦点下标
    private int mFocusIndex;
    //是否是key值
    private boolean mIsKey;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public BiddingCargoPublishContactAdapter(List<CargoPublishEntity.ContactInfoEntity> data) {
        super(data);
        addItemType(SingleItemConstant.TYPE_ONE, R.layout.item_bidding_cargo_publish_contact);
        addItemType(SingleItemConstant.TYPE_TWO, R.layout.item_bidding_cargo_publish_contact_add);
    }

    @Override
    protected void convert(BaseViewHolder helper, CargoPublishEntity.ContactInfoEntity item) {
        if (item.getItemType() == SingleItemConstant.TYPE_TWO) {
            helper.setText(R.id.tv_release_cargo_no, String.valueOf(helper.getLayoutPosition() + 1))
                    .addOnClickListener(R.id.iv_release_cargo_del);
        }
        helper.setText(R.id.et_release_cargo_contact, DefaultUtil.getString(item.getContrcts()))
                .setText(R.id.et_release_cargo_contact_tel, DefaultUtil.getString(item.getContrctsNumber()));
        //设置监听
        initEvent(helper);
    }

    private void initEvent(BaseViewHolder helper) {
        //设置文字监听
        EditText contact = helper.getView(R.id.et_release_cargo_contact);
        EditText tel = helper.getView(R.id.et_release_cargo_contact_tel);
        //添加焦点监听
        contact.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                mIsKey = true;
                mFocusIndex = helper.getLayoutPosition();
                contact.addTextChangedListener(textWatcher);
                //防止点击清空按钮时内容没修改
                changeContent(contact.getText());
            } else {
                contact.removeTextChangedListener(textWatcher);
            }
        });
        tel.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                mIsKey = false;
                mFocusIndex = helper.getLayoutPosition();
                tel.addTextChangedListener(textWatcher);
                //防止点击清空按钮时内容没修改
                changeContent(tel.getText());
            } else {
                tel.removeTextChangedListener(textWatcher);
            }
        });
    }


    /**
     * 输入框文字输入监听
     */
    private final TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            changeContent(s);
        }
    };

    /**
     * 改变内容
     */
    private void changeContent(CharSequence s) {
        CargoPublishEntity.ContactInfoEntity item = mData.get(mFocusIndex);
        if (mIsKey) {
            item.setContrcts(s.toString());
        } else {
            item.setContrctsNumber(s.toString());
        }
    }
}
